<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/23
 * Time: 18:47
 */

namespace App\Game;

use App\Models\Def\MainGamePlat;
use App\Models\Inf\Player;
use App\Models\OddsHandicap;
use Carbon\Carbon;
use App\Game\Traits\VrTrait;
use Illuminate\Http\Request;

class vr extends Game
{
    use VrTrait;

//    public $timezone = 'GMT-4';
    private $platName = 'vr';



    public function __construct()
    {

        $this->header = [
            "Accept" => "application/json",
            "Content-Type" => "application/json",
        ];
        $this->adds = [
            'main_game_plat_code' => $this->platName,
        ];
    }

    public function getLength()
    {
        return $this->length;
    }

    public function getPre()
    {
        return $this->pre;
    }

    /**
     * gameapi
     * 2018/9/19 16:15
     * Administrator
     * __get
     *
     * @param string $key
     * @return mixed
     */
    public function __get($key)
    {
        return $this->$key;
    }

    public function __set($key, $value)
    {
        $this->$key = $value;
    }


    /**
     * gameapi
     * 2018/9/24 14:16
     * Administrator
     * curlCreateMember
     * 创建用户
     * @param $accountUserName
     * @param $password
     * @param $request
     * @return array
     */
    public function curlCreateMember($accountUserName, $password, $request)
    {

        $action = [
            'platform' => 'VR',
            'method' => 'register',
            'username' => $accountUserName,
            'password' => $password,
            'usertype' => $this->usertype,
            'currency' => 'CNY',
            'lang' => 'ZH-CN',
        ];

        $output = $this->agRequest($action);

        if ($output['status']==0) {
            return ['success' => false, 'data' => [], 'message' => '创建用户失败，错误原因：CURL 未能执行成功 ' . $output['data']];
        }

        if ($output['header']==0) {
            return ['success' => false, 'data' => [], 'message' => '创建用户失败，错误原因：对方服务器 未能执行成功' . $output['data']];
        }
        $data = $this->strToArray($output['data']);

        if (!is_array($data) || $data['code'] != 0){
            return ['success' => false, 'data' => [], 'message' => '创建用户失败，错误原因：'.$data['message']];
        }
        return ['success' => true, 'data' => ['accountUserName' => $accountUserName, 'password' => $password], 'message' => ''];
    }

    /**
     * gameapi
     * 2018/9/24 14:15
     * Administrator
     * curlGetBalance
     * 查询余额
     * @param $accountUserName
     * @return array
     */
    public function curlGetBalance($accountUserName,$request)
    {
        return ['success' => false, 'data' => [], 'message' => '该类型为免转钱包类型，无需查询'];
    }


    //转入游戏
    public function curlTransferIn($accountUserName, $price, $transferid,$mainGamePlatId)
    {
        return ['success' => true, 'data' => ['transferid' => $this->cagent . $transferid, 'transfer_state' => 1], 'message' => ''];
    }

    //转出游戏
    public function curlTransferTo($accountUserName, $price, $transferid,$mainGamePlatId)
    {
        return ['success' => true, 'data' => ['transferid' => $this->cagent . $transferid, 'transfer_state' => 1], 'message' => ''];
    }

    //查询订单状态
    public function curlCheckTransfer($request)
    {
        $action = [
            'command' => 'queryorderstatus',
            'parameters' => [
                'billno' => $request->transferid,
                'cur' => 'CNY'
            ]
        ];

        $output = $this->agRequest($action);
        $output = json_decode($output, true);

        if (array_key_exists('result', $output)) {
            if ($output['status'] == 200) {
                $result = $this->xmlToArray($output['result']);
                if ($result['@attributes']['info'] == 0) {
                    return ['success' => true, 'data' => ['transfer_state'=>1], 'message' => ''];
                } elseif($result['@attributes']['info'] == 1 || $result['@attributes']['info'] == 2) {
                    return ['success' => false, 'data' => ['transfer_state'=>0], 'message' => '转账失败，查询失败原因：' . $result['@attributes']['msg']];
                }else{
                    return ['success' => false, 'data' => ['transfer_state'=>-1], 'message' => '转账失败，查询失败原因：' . $result['@attributes']['msg']];
                }
            } else {
                return ['success' => false, 'data' => ['transfer_state'=>-1], 'message' => '查询订单失败，错误原因：' . $output['result']];
            }
        } else {
            return ['success' => false, 'data' => ['transfer_state'=>-1], 'message' => '请求平台失败，状态未知'];
        }
    }

    //踢线
    public function curlKick(Request $request)
    {
        $mainGamePlat = MainGamePlat::where('main_game_plat_code', $request->mainGamePlat)->first();
        $playGameAccount = Player::where('username', $request->accountUserName)->where('main_game_plats_id', $mainGamePlat->id)->first();
        $action = [
            'platform' => 'VR',
            'method' => 'logout',
            'username' => $request->accountUserName,
            'password' => $playGameAccount->password,
        ];
        $output = $this->agRequest($action);
        if ($output['status']==0) {
            return ['success' => false, 'data' => [], 'message' => '踢线失败，错误原因：CURL 未能执行成功 ' . $output['data']];
        }

        if ($output['header']==0) {
            return ['success' => false, 'data' => [], 'message' => '踢线失败，错误原因：对方服务器 未能执行成功' . $output['data']];
        }
        $data = $this->strToArray($output['data']);
        if (!is_array($data) || $data['code'] != 0){
            return ['success' => false, 'data' => [], 'message' => '踢线失败，错误原因：'.$data['message']];
        }
        return ['success' => true, 'data' => [], 'message' => ''];
    }

    /**
     * gameapi
     * 2018/9/24 16:35
     * Administrator
     * curlJoinGame
     * 进入游戏
     * @param $request
     * @return array
     */
    public function curlJoinGame($request)
    {
        $mainGamePlat = MainGamePlat::where('main_game_plat_code', $request->mainGamePlat)->first();
        if (isset($request->odds_handicap)) {
            $oddType = OddsHandicap::where([
                    ['code', '=', $request->odds_handicap],
                    ['plat_id', '=', $mainGamePlat->id],
                ]
            )->first();
        }
        $playerOdds = isset($oddType) ? $oddType->odds_value : '';  //赔率

        $action = [
            'platform' => 'VR',
            'method' => 'login',
            'username' => $request->accountUserName,
            'password' => $request->password,
            'game' => 0,
            'odds' => $playerOdds,
            'url' => $this->backUrl,
        ];

        $output = $this->agRequest($action);

        if ($output['status']==0) {
            return ['success' => false, 'data' => [], 'message' => '进入游戏，错误原因：CURL 未能执行成功 ' . $output['data']];
        }

        if ($output['header']==0) {
            return ['success' => false, 'data' => [], 'message' => '进入游戏，错误原因：对方服务器 未能执行成功' . $output['data']];
        }
        $data = $this->strToArray($output['data']);
        if (!is_array($data) || $data['code'] != 0){
            return ['success' => false, 'data' => [], 'message' => '进入游戏，错误原因：'.$data['message']];
        }
        return ['success' => true, 'data' => ['url' => $data['data']], 'message' => ''];
    }


    /**
     * gameapi
     * 2018/9/24 16:35
     * Administrator
     * curlJoinMobileGame
     * 手机端进入游戏
     * @param $request
     * @return array
     */
    public function curlJoinMobileGame($request)
    {
        $mainGamePlat = MainGamePlat::where('main_game_plat_code', $request->mainGamePlat)->first();
        if (isset($request->odds_handicap)) {
            $oddType = OddsHandicap::where([
                    ['code', '=', $request->odds_handicap],
                    ['plat_id', '=', $mainGamePlat->id],
                ]
            )->first();
        }
        $playerOdds = isset($oddType) ? $oddType->odds_value : '';  //赔率

        $action = [
            'platform' => 'VR',
            'method' => 'login',
            'username' => $request->accountUserName,
            'password' => $request->password,
            'game' => 0,
            'odds' => $playerOdds,
            'url' => $this->backUrl,
        ];

        $output = $this->agRequest($action);

        if ($output['status']==0) {
            return ['success' => false, 'data' => [], 'message' => '进入游戏，错误原因：CURL 未能执行成功 ' . $output['data']];
        }

        if ($output['header']==0) {
            return ['success' => false, 'data' => [], 'message' => '进入游戏，错误原因：对方服务器 未能执行成功' . $output['data']];
        }
        $data = $this->strToArray($output['data']);
        if (!is_array($data) || $data['code'] != 0){
            return ['success' => false, 'data' => [], 'message' => '进入游戏，错误原因：'.$data['message']];
        }
        return ['success' => true, 'data' => ['url' => $data['data']], 'message' => ''];
    }


    //游戏抓单
    public function curlGetBetRecord(Carbon $startTime, Carbon $endTime)
    {
        $data = [];
        $start_time = $startTime->timestamp;
        $end_time = $endTime->timestamp;
//        file_put_contents('/home/gameapi/storage/logs/test.log', 'start...'.$startTime->toDateTimeString().PHP_EOL, FILE_APPEND);
//        file_put_contents('/home/gameapi/storage/logs/test.log', 'end...'.$endTime->toDateTimeString().PHP_EOL, FILE_APPEND);
//        $start_time = 1563696000;
//        $end_time = 1563699600;
        foreach ($this->platformTypes as $platformType) {
            $pageNumber = 1;
            do {
                $flag = false;
                $action = [
                    'platform' => 'VR',
                    'method' => 'raw',
                    'start' => $start_time,
                    'end' => $end_time,
                    'page' => $pageNumber,
                    'status' => 0,
                    'type' => strtolower($platformType),
                ];
                $output = $this->agRequest($action);
                if (is_array($output) && $output['status']==1 && $output['header']==1) {
                    $result = $this->strToArray($output['data']);
                    if (is_array($result) && $result['code'] == 0 && $pageNumber <= $result['data']['pageAmount']) {
                        if ($result['data']['list']){
                            $data = array_merge($data, $result['data']['list']);
                            $flag = true;
                        }
                    }
                }
                $pageNumber++;
            } while ($flag);
        }
//        file_put_contents('/home/gameapi/storage/logs/test.log', 'aa...'.json_encode($data).PHP_EOL, FILE_APPEND);
        return $data;
    }
}